﻿using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using NewLife;
using PMS.WebApi.Attributes;
using PMS.WebApi.Common;
using PMS.WebApi.Models;
using PMS.WebApi.Utils;
using Swashbuckle.AspNetCore.Annotations;

namespace PMS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [SwaggerTag("控制器", "监控平台接口")]
    public class AuthController : ControllerBase
    {
        // POST api/<MonitoringController>        
        [HttpPost]
        [ApiJwtAllowAnonymousFilter]
        [SwaggerOperation("上报数据", "将采集到的性能数据上报到监控平台")]
        public dynamic GetToken([FromBody][Required] AccountInfo account)
        {
            try
            {
                if (!account.username.EqualIgnoreCase(SysConst.API_User) || !account.password.Equals(SysConst.API_UserPwd))
                    return "Incorrect username or password!".ToErrorResult();
                AuthInfo auth = new AuthInfo { username = SysConst.API_User, roles = new List<string> { "Reporter" }, is_admin = false };
                var token = JwtHelper.SetJwtEncode(auth.username, 60, auth);
                return token.ToSuccessResult();
            }
            catch (Exception ex)
            {
                return ex.Message.ToErrorResult();
            }
        }
    }
}
